package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiGoodsActTypeRes;
import cn.com.api.entity.ApiT_goods_activity;

import cn.com.api.service.ApiGoodsActTypeService;
import cn.com.api.service.ApiT_goods_activityService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "activity/")
public class ApiT_goods_activityController {
	@Autowired
	private ApiT_goods_activityService apiT_goods_activityService;
	@Autowired
	private ApiGoodsActTypeService apiGoodsActTypeService;

	private static Logger logger = Logger
			.getLogger(ApiT_goods_activityController.class);
	
	/**
	 * 查询充值有礼的商品
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getChargeGoodsByCityId")
	public void getChargeGoodsByCityId(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String city_id = request.getParameter("city_id");
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			apiT_goods_activityService.searchChargeActivityGoods(map, city_id);
		}catch(Exception e){
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_goods_activity")
	public void insertT_goods_activity(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String act_id = request.getParameter("act_id");
		String act_name = request.getParameter("act_name");
		String act_desc = request.getParameter("act_desc");
		String act_type_id = request.getParameter("act_type_id");
		String city_id = request.getParameter("city_id");
		String goods_id = request.getParameter("goods_id");
		String product_id = request.getParameter("product_id");
		String goods_name = request.getParameter("goods_name");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String is_finished = request.getParameter("is_finished");
		String ext_info = request.getParameter("ext_info");
		String act_img = request.getParameter("act_img");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (act_type_id != null && !"".equals(act_type_id.trim())) {
				ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();

				t_goods_activity.setAct_name(act_name);
				t_goods_activity.setAct_desc(act_desc);
				t_goods_activity.setExt_info(ext_info);
				t_goods_activity.setAct_img(act_img);

				apiT_goods_activityService
						.insertT_goods_activity(t_goods_activity);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_goods_activityById")
	public void findT_goods_activityById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_goods_activity t_goods_activity = apiT_goods_activityService
					.findT_goods_activityById(Integer.valueOf(id));

			// return
			map.put("datas", t_goods_activity);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_goods_activitys")
	public void findT_goods_activitys(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityService
					.findT_goods_activitys();

			// return
			map.put("datas", t_goods_activitys);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_goods_activitysByActTypeId")
	public void findT_goods_activitysByActTypeId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String act_type_id = request.getParameter("act_type_id");
		String cityid = request.getParameter("cityid");
		String pageCount = request.getParameter("pageCount");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (act_type_id != null && !"".equals(act_type_id.trim())) {
				PageInfo page = new PageInfo();
				page.setCurrentPage(Integer.parseInt(pageCount));// 设置要读取的页
				map = apiT_goods_activityService
						.findT_goods_activitysByActTypeId(
								Integer.valueOf(cityid), act_type_id, page);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_goods_activity")
	public void updateT_goods_activity(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String act_id = request.getParameter("act_id");
		String act_name = request.getParameter("act_name");
		String act_desc = request.getParameter("act_desc");
		String act_type_id = request.getParameter("act_type_id");
		String city_id = request.getParameter("city_id");
		String goods_id = request.getParameter("goods_id");
		String product_id = request.getParameter("product_id");
		String goods_name = request.getParameter("goods_name");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String is_finished = request.getParameter("is_finished");
		String ext_info = request.getParameter("ext_info");
		String act_img = request.getParameter("act_img");

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiT_goods_activity t_goods_activity = new ApiT_goods_activity();
				// 获取原数据
				t_goods_activity = apiT_goods_activityService
						.findT_goods_activityById(Integer.valueOf(id));
				// 修改

				//
				int ret_i = apiT_goods_activityService
						.updateT_goods_activity(t_goods_activity);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_goods_activity")
	public void deleteT_goods_activity(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiT_goods_activityService
						.deleteT_goods_activity(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 
	 * 分类活动
	 */
	@RequestMapping(value = "goodsActByType")
	public void goodsActByType(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String cityid=request.getParameter("cityid");
		Map<String,Object> res=new HashMap<String,Object>();
		List<Map<String, String>> big=new ArrayList<Map<String,String>>();
		List<Map<String,List<ApiGoodsActTypeRes>>> map=new ArrayList<Map<String,List<ApiGoodsActTypeRes>>>();
		big=apiGoodsActTypeService.goodsActByCity(cityid);
		for(Map<String,String> m:big){
			Map<String,List<ApiGoodsActTypeRes>> avgObj=new HashMap<String,List<ApiGoodsActTypeRes>>();
			List<ApiGoodsActTypeRes> info=new ArrayList<ApiGoodsActTypeRes>();
			info=apiGoodsActTypeService.goodsActInfo(m.get("typeid"), m.get("cityid"), m.get("createtime"));
			avgObj.put("data",info);
			map.add(avgObj);
		}
		res.put("datas", map);
		res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		res.put(Utils.MESSAGE, "查询成功！");
		if(map.size()>0){
			res.put("is", "1");//有数据
		}else{
			res.put("is", "0");//没数据
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(res));
		out.close();
	}
	
}
